package com.osmp.web.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;


/**      
 * request 对象的相关操作
 * @author zhangtengda        
 * @version 1.0      
 * @created 2015年5月2日 下午8:25:43     
 */       
public class RequestUtils {

    /***
     * 获取 request 中 json 字符串的内容
     * 
     * @param request
     * @return : <code>byte[]</code>
     * @throws IOException
     */
    public static String getRequestJsonString(HttpServletRequest request)
            throws IOException {
        String submitMehtod = request.getMethod();
        // GET
        if (submitMehtod.equals("GET")) {
            return new String(request.getQueryString().getBytes("iso-8859-1"),"utf-8").replaceAll("%22", "\"");
        // POST
        } else {
            return getRequestPostStr(request);
        }
    }

    /**      
     * 描述:获取 post 请求的 byte[] 数组
     * <pre>
     * 举例：
     * </pre>
     * @param request
     * @return
     * @throws IOException      
     */
    public static byte[] getRequestPostBytes(HttpServletRequest request)
            throws IOException {
        int contentLength = request.getContentLength();
        if(contentLength<0){
            return null;
        }
        byte buffer[] = new byte[contentLength];
        for (int i = 0; i < contentLength;) {

            int readlen = request.getInputStream().read(buffer, i,
                    contentLength - i);
            if (readlen == -1) {
                break;
            }
            i += readlen;
        }
        return buffer;
    }

    /**      
     * 描述:获取 post 请求内容
     * <pre>
     * 举例：
     * </pre>
     * @param request
     * @return
     * @throws IOException      
     */
    public static String getRequestPostStr(HttpServletRequest request)
            throws IOException {
        byte buffer[] = getRequestPostBytes(request);
        String charEncoding = request.getCharacterEncoding();
        if (charEncoding == null) {
            charEncoding = "UTF-8";
        }
        return new String(buffer, charEncoding);
    }
    
    public static <T> T getEntity(Class<T> clazz, HttpServletRequest request){
    	String json = null;
		try {
			json = URLDecoder.decode(getRequestJsonString(request), "UTF-8");
			json = JSONObject.toJSONString(getMap(json));
		} catch (IOException e) {
			e.printStackTrace();
		}
    	T t = JSONObject.parseObject(json, clazz);
    	return t;
    }

    public static Map<String,String> getMap (String url) {
    	Map<String,String> map = new HashMap<>();
        String [] strArr = url.split("&");
        for(String i : strArr){
        	String [] param = i.split("=");
        	if(param.length == 2){
        		map.put(param[0], param[1]);
        	}else{
        		map.put(param[0], "");
        	}
        }
        return map;  
    }
    
    public static Map<String,Object> SUCCESS(){
    	Map<String,Object> result = new HashMap<>();
    	result.put("statusCode", 200);
    	result.put("title", "操作提示");
    	result.put("message", "恭喜你，操作成功！");
    	return result;
    }
    
    public static Map<String,Object> ERROR(){
    	Map<String,Object> result = new HashMap<>();
    	result.put("statusCode", 300);
    	result.put("title", "操作提示");
    	result.put("message", "对不起，操作失败！");
    	return result;
    }
    
    public static Map<String,Object> ERROR(String message){
    	Map<String,Object> result = new HashMap<>();
    	result.put("statusCode", 300);
    	result.put("title", "操作提示");
    	result.put("message", message);
    	return result;
    }
}